package gcu.module.property_money.dao;

import gcu.module.property_money.dao.mapper.MoneyMapper;
import gcu.module.property_money.daomain.PropertyMoney;
import gcu.util.Pager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by haol on 2016/9/25.
 */
@Repository
public class MoneyDao {
      @Autowired
      private MoneyMapper mapper;

      private Logger logger = LoggerFactory.getLogger(MoneyDao.class);

      public void addMoney(PropertyMoney money) {
            try {
                  mapper.addMoney(money);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
            }
      }

      public void updateMoney(PropertyMoney money) {
            try {
                  mapper.updateMoney(money);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
            }
      }

      public void deleteMoney(int id) {
            try {
                  mapper.deleteMoneyByOwnerId(id);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
            }
      }

      public List<PropertyMoney> queryMoneyList(PropertyMoney money, Pager pager) {
            try {
                  int skip = (pager.getCurrentPage() - 1) * pager.getPageSize();
                  int pageSize = pager.getPageSize();
                  pager.setTotalSize(mapper.count(money));
                  return mapper.query(money, skip, pageSize);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
                  return null;
            }
      }

      public PropertyMoney queryMoneyById(Integer id) {
            try {
                  return mapper.queryMoneyById(id);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
                  return null;
            }
      }

      public PropertyMoney queryMoneyByOwnerId(Integer ownerId) {
            try {
                  return mapper.queryMoneyByOwnerId(ownerId);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
                  return null;
            }
      }

      public void freshenMoney(String date) {
            try {
                  mapper.freshenMoney_isIn(date);
                  mapper.freshenMoney_isNot(date);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
            }
      }

}
